
<?php

require 'config_inc.php';
require 'dal_common.php';
require 'dal_account.php';
require 'api_common.php';

$t_args = get_params(Array(
	'username' => '+username',
	'password' => '+any',
	'device' => '-device'
));
if ($t_args == null) {
	echo 'ERROR=INVALID_PARAMETER;';
	return;
}

$t_uinfo = dal_account_get_userinfo_by_username($t_args['username']);
if ($t_uinfo != NULL) {
	echo "ERROR=ALREADY_EXISTED";
	return;
}

if ($t_args['device'] != '') {
	$t_uinfo = dal_account_get_userinfo_by_device($t_args['device']);
	if ($t_uinfo != NULL) {
		if (!dal_account_bind_username($t_args['device'], $t_args['username'], $t_args['password'])) {
			echo "ERROR=SYSTEM_ERROR;";
		} else {
			echo "ERROR=0;";
		}
		return;
	}
}

if (!dal_account_register($t_args['username'], $t_args['password'])) {
	echo "ERROR=SYSTEM_ERROR;";
	return;
}

echo "ERROR=0;";
?>
